fixes for the position and track filters. (#157)
authortsteven4 <tsteven4@users.noreply.github.com>
Sat, 30 Dec 2017 23:21:12 +0000 (16:21 -0700)
committerGitHub <noreply@github.com>
Sat, 30 Dec 2017 23:21:12 +0000 (16:21 -0700)
the position filter could corrupt the route_waypt_ct for routes/tracks.
the track filter underestimated speed when there were multiple
waypoints with the same time.

position.cc
trackfilter.cc

index 38ce7c0a282d29421d0985ee0183ceaf039391be..0d5d54725ad2443d4507ab02ad601a6d873df2db 100644 (file)
@@ -87,7 +87,7 @@ position_runqueue(queue* q, int nelems, int qtype)
   qlist = (int*) xcalloc(nelems, sizeof(*qlist));
 
 #if NEWQ
-  foreach(Waypoint* waypointp, waypt_list) {
+  foreach (Waypoint* waypointp, waypt_list) {
     comp[i] = waypointp;
 #else
   QUEUE_FOR_EACH(q, elem, tmp) {
@@ -166,18 +166,30 @@ position_runqueue(queue* q, int nelems, int qtype)
 }
 
 static void
-position_process_route(const route_head* rh)
+position_process_any_route(const route_head* rh, int type)
 {
   int i = rh->rte_waypt_ct;
 
   if (i) {
     cur_rte = (route_head*)rh;
-    position_runqueue((queue*)&rh->waypoint_list, i, rtedata);
+    position_runqueue((queue*)&rh->waypoint_list, i, type);
     cur_rte = NULL;
   }
 
 }
 
+static void
+position_process_rte(const route_head* rh)
+{
+  position_process_any_route(rh, rtedata);
+}
+
+static void
+position_process_trk(const route_head* rh)
+{
+  position_process_any_route(rh, trkdata);
+}
+
 static void
 position_noop_w(const Waypoint*)
 {
@@ -196,8 +208,8 @@ void position_process()
     position_runqueue(&waypt_head, i, wptdata);
   }
 
-  route_disp_all(position_process_route, position_noop_t, position_noop_w);
-  track_disp_all(position_process_route, position_noop_t, position_noop_w);
+  route_disp_all(position_process_rte, position_noop_t, position_noop_w);
+  track_disp_all(position_process_trk, position_noop_t, position_noop_w);
 }
 
 void
index 601ae0ab8caa08ad587b8fd2d2da52085a0c4a15..40ff8f302cb9d7b9f14bf2dd304affe827716fe6 100644 (file)
@@ -817,10 +817,12 @@ trackfilter_synth()
   queue* elem, *tmp;
   Waypoint* wpt;
 
-  double oldlat = -999;
-  double oldlon = -999;
-  time_t oldtime = 0;
-  int first = 1;
+  double last_course_lat;
+  double last_course_lon;
+  double last_speed_lat;
+  double last_speed_lon;
+  time_t last_speed_time;
+  int first;
   fix_type fix;
   int nsats = 0;
 
@@ -839,33 +841,50 @@ trackfilter_synth()
       }
       if (first) {
         if (opt_course) {
+          // TODO: the course value 0 isn't valid, wouldn't it be better to UNSET course?
           WAYPT_SET(wpt, course, 0);
         }
         if (opt_speed) {
+          // TODO: the speed value 0 isn't valid, wouldn't it be better to UNSET speed?
           WAYPT_SET(wpt, speed, 0);
         }
         first = 0;
+        last_course_lat = wpt->latitude;
+        last_course_lon = wpt->longitude;
+        last_speed_lat = wpt->latitude;
+        last_speed_lon = wpt->longitude;
+        last_speed_time = wpt->GetCreationTime().toTime_t();
       } else {
         if (opt_course) {
-          WAYPT_SET(wpt, course, heading_true_degrees(RAD(oldlat),
-                    RAD(oldlon),RAD(wpt->latitude),
+          WAYPT_SET(wpt, course, heading_true_degrees(RAD(last_course_lat),
+                    RAD(last_course_lon),RAD(wpt->latitude),
                     RAD(wpt->longitude)));
+          last_course_lat = wpt->latitude;
+          last_course_lon = wpt->longitude;
         }
         if (opt_speed) {
-          if (oldtime != wpt->GetCreationTime().toTime_t()) {
+          if (last_speed_time != wpt->GetCreationTime().toTime_t()) {
+            // If we have mutliple points with the same time and
+            // we use the pair of points about which the time ticks then we will
+            // underestimate the distance and compute low speeds on average.
+            // Therefore, if we have multiple points with the same time use the
+            // first ones with the new times to compute speed.
+            // Note that points with the same time can occur because the input
+            // has truncated times, or because we are truncating times with
+            // toTime_t().
             WAYPT_SET(wpt, speed, radtometers(gcdist(
-                                                RAD(oldlat), RAD(oldlon),
+                                                RAD(last_speed_lat), RAD(last_speed_lon),
                                                 RAD(wpt->latitude),
                                                 RAD(wpt->longitude))) /
-                      labs(wpt->GetCreationTime().toTime_t()-oldtime));
+                      labs(wpt->GetCreationTime().toTime_t()-last_speed_time));
+            last_speed_lat = wpt->latitude;
+            last_speed_lon = wpt->longitude;
+            last_speed_time = wpt->GetCreationTime().toTime_t();
           } else {
             WAYPT_UNSET(wpt, speed);
           }
         }
       }
-      oldlat = wpt->latitude;
-      oldlon = wpt->longitude;
-      oldtime = wpt->GetCreationTime().toTime_t();
     }
   }
 }